const mongoose = require('mongoose')
const data = require('../student.json')
mongoose.connect('mongodb://127.0.0.1:27017/abc_1')


mongoose.connection.once('open', async () => {
    console.log("连接成功")
    // 字段验证
    let schema = {
        student_id: String,
        name: String,
        gender: String,
        age: Number,
        grade: String,
        major: String,
        email: String,
        phone: String,
        address: String,
        gpa: Number,
        courses: Array
    }
    let studentSchema = new mongoose.Schema(schema)
    const Model = mongoose.model('students', studentSchema)


    try {
        // 删除单条：deleteOne(条件)
        // let res = await Model.deleteOne({ student_id: "20230001" })

        // 批量删除：deleteMany(条件)
        // let res = await Model.deleteMany({ age: 19 })

        // 更新单条：updateOne(条件)
        // let res = await Model.updateOne({ student_id: 20230003 }, { name: 'zhangsan' })

        // 批量更新：updateMany(条件)
        // let res = await Model.updateMany({ gender: '女' }, { age: 18 })

        // 查询单条：findOne(条件)
        // let res = await Model.findOne({ name: '韩梅' })

        // 根据id查询：findById(id)
        // let res = await Model.findById('6800d96c29ebd493036aa532')

        // 查询所有：传入条件按条件查询多条、不传入条件查询全部
        let res = await Model.find()

        // 新增一条
        // let res = await Model.create(data[0])

        // 批量新增
        // let res = await Model.create(data)

        console.log(res);
    } catch (e) {
        console.log(e);
    }


    // console.log(await Model.insertMany(data));


})


